#include "tlv5618a.h"

//https://blog.csdn.net/qq_35872856/article/details/77206176
//https://github.com/ZS6TVB/Arduino-APRS-Digipeater/blob/9c1ec6c7013e32f9805ef479169c4cb4de35cc2a/Software/APRS%20Digi/bertos/drv/tlv5618.c

#define POWERDOWN 0x2000
#define OUTA 0xC000
#define OUTB 0x4000

static void tlv5618_write(struct Tlv5618 *ctx, uint16_t val)
{
	//TLV5618_CSLOW(ctx->cs_pin);
	//kfile_putc(val >> 8, ctx->ch);
	//kfile_putc(val & 0xFF, ctx->ch);
	//kfile_flush(ctx->ch);
	//TLV5618_CSHIGH(ctx->cs_pin);
	ctx->setCs(0);
	ctx->spiTransmit(val);
	ctx->setCs(1);
}

void tlv5618_setOutA(struct Tlv5618 *ctx, uint16_t val)
{
	val &= 0x0FFF;
	tlv5618_write(ctx, val | OUTA);
}

void tlv5618_setOutB(struct Tlv5618 *ctx, uint16_t val)
{
	val &= 0x0FFF;
	tlv5618_write(ctx, val | OUTB);
}

void tlv5618_setPowerDown(struct Tlv5618 *ctx)
{
	tlv5618_write(ctx, POWERDOWN);
}

void tlv5618_init(struct Tlv5618 *ctx, void (*setCs)(uint8_t), void (*spiTransmit)(uint16_t))
{
	ctx->setCs = setCs;
	ctx->spiTransmit = spiTransmit;

	tlv5618_setPowerDown(ctx);
}
